package com.ruoyi.manage.enterprise.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StrUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import com.ruoyi.manage.enterprise.domain.EcEnterpriseOperator;
import com.ruoyi.manage.enterprise.domain.vo.EcEnterpriseOperatorVo;
import com.ruoyi.manage.enterprise.service.IEcEnterpriseOperatorService;
import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * 经办人Controller
 *
 * @author ruoyi
 * @date 2025-08-13
 */
@Api(tags="经办人控制器")
@RestController
@RequestMapping("/manage/operator" )
public class EcEnterpriseOperatorController extends BaseController {
    /**
    * 配置表别名 默认空
    */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private IEcEnterpriseOperatorService ecEnterpriseOperatorService;

    /**
     * 查询经办人列表
     */
    @ApiOperation(value = "查询经办人",notes = "经办人管理")
    @PreAuthorize("@ss.hasPermi('manage:operator:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(EcEnterpriseOperatorVo ecEnterpriseOperator,QueryWrapper<EcEnterpriseOperator> queryWrapper) {
        setQueryCondition(ecEnterpriseOperator,queryWrapper);
        startPage();
        List<EcEnterpriseOperator> list = ecEnterpriseOperatorService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(EcEnterpriseOperatorVo ecEnterpriseOperator, QueryWrapper<EcEnterpriseOperator> queryWrapper) {
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    /**
     * 导出经办人列表
     */
    @PreAuthorize("@ss.hasPermi('manage:operator:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, EcEnterpriseOperatorVo ecEnterpriseOperator,QueryWrapper<EcEnterpriseOperator> queryWrapper) {
        if (CollUtil.isEmpty( ecEnterpriseOperator.getIds())){
            setQueryCondition(ecEnterpriseOperator,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",ecEnterpriseOperator.getIds());
        }
        List<EcEnterpriseOperator> list = ecEnterpriseOperatorService.list(queryWrapper);
        ExcelUtil<EcEnterpriseOperator> util = new ExcelUtil<EcEnterpriseOperator>(EcEnterpriseOperator. class);
        util.exportExcel(response,list, "operator");
    }

    /**
     * 获取经办人详细信息
     */
    @ApiOperation(value = "获取经办人",notes = "经办人管理")
    @PreAuthorize("@ss.hasPermi('manage:operator:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(ecEnterpriseOperatorService.queryGetById(id));
    }

    /**
     * 新增经办人
     */
    @ApiOperation(value = "新增经办人",notes = "经办人管理")
    @PreAuthorize("@ss.hasPermi('manage:operator:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody EcEnterpriseOperator ecEnterpriseOperator) {
        return toAjax(ecEnterpriseOperatorService.addEcEnterpriseOperator(ecEnterpriseOperator) ? 1 : 0);
    }

    /**
     * 修改经办人
     */
    @ApiOperation(value = "修改经办人",notes = "经办人管理")
    @PreAuthorize("@ss.hasPermi('manage:operator:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody EcEnterpriseOperator ecEnterpriseOperator) {
        return toAjax(ecEnterpriseOperatorService.updateEcEnterpriseOperator(ecEnterpriseOperator) ? 1 : 0);
    }

    /**
     * 删除经办人
     */
    @ApiOperation(value = "删除经办人",notes = "经办人管理")
    @PreAuthorize("@ss.hasPermi('manage:operator:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ecEnterpriseOperatorService.deleteEcEnterpriseOperatorByIds(ids) ? 1 : 0);
    }
}
